package com.andy.module.uploadphoto

import android.widget.ImageView
import com.andy.module.R
import com.andy.ui.view.RoundProgressBar
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder


/**
 * @creator:LuoFan
 * @date:2022/6/10
 * @description:
 */
class UploadPhotoAdapter(layout: Int = R.layout.adapter_upload_photo) : BaseQuickAdapter<UploadPhotoBean, BaseViewHolder>(layout) {

    init {
        addChildClickViewIds(R.id.img_photo, R.id.img_add, R.id.img_retry, R.id.img_delete)
    }

    override fun convert(holder: BaseViewHolder, item: UploadPhotoBean) {
        val photoImg = holder.getView<ImageView>(R.id.img_photo)
        if (item.photoUrl.isNotEmpty()) {
            Glide.with(context).load(item.photoUrl).centerCrop().into(photoImg)
        } else if (item.localPath.isNotEmpty()) {
            Glide.with(context).load(item.localPath).centerCrop().into(photoImg)
        } else {
            Glide.with(context).load("").centerCrop().into(photoImg)
        }
        when (item.status) {
            UploadPhotoBean.STATUS_NONE -> {
                holder.setVisible(R.id.img_add, true)
                holder.setVisible(R.id.img_retry, false)
                holder.setVisible(R.id.img_delete, false)
                holder.setVisible(R.id.upload_progress, false)
                holder.setVisible(R.id.tv_error, false)
            }
            UploadPhotoBean.STATUS_UPLOADING -> {
                holder.setVisible(R.id.img_add, false)
                holder.setVisible(R.id.img_retry, false)
                //上传中点击删除取消上传
                holder.setVisible(R.id.img_delete, true)
                holder.setVisible(R.id.upload_progress, true)
                holder.setVisible(R.id.tv_error, false)
            }
            UploadPhotoBean.STATUS_UPLOAD_SUCCESS -> {
                holder.setVisible(R.id.img_add, false)
                holder.setVisible(R.id.img_retry, false)
                holder.setVisible(R.id.img_delete, true)
                holder.setVisible(R.id.upload_progress, false)
                holder.setVisible(R.id.tv_error, false)
                val progressView = holder.getView<RoundProgressBar>(R.id.upload_progress)
                progressView.progress = item.uploadPercent.toInt()
            }
            UploadPhotoBean.STATUS_UPLOAD_ERROR -> {
                holder.setVisible(R.id.img_add, false)
                holder.setVisible(R.id.img_retry, true)
                holder.setVisible(R.id.img_delete, true)
                holder.setVisible(R.id.upload_progress, false)
                holder.setVisible(R.id.tv_error, true)
            }
        }
    }


    fun addEmptyPhoto() {
        addData(UploadPhotoBean())
    }

    //新增本地图片
    fun addUploadingPhoto(localPath: String, position: Int) {
        val bean = UploadPhotoBean()
        bean.status = UploadPhotoBean.STATUS_UPLOADING
        bean.localPath = localPath
        addData(position, bean)
    }

    fun updateItemSuccessByPath(localPath: String, photoUrl: String) {
        data.forEachIndexed { index, uploadPhotoBean ->
            if (localPath == uploadPhotoBean.localPath) {
                uploadPhotoBean.status = UploadPhotoBean.STATUS_UPLOAD_SUCCESS
                uploadPhotoBean.photoUrl = photoUrl
                notifyItemChanged(index, uploadPhotoBean)
            }
        }
        notifyDataSetChanged()
    }

    fun updateItemErrorByPath(localPath: String) {
        data.forEachIndexed { index, uploadPhotoBean ->
            if (localPath == uploadPhotoBean.localPath) {
                uploadPhotoBean.status = UploadPhotoBean.STATUS_UPLOAD_ERROR
                notifyItemChanged(index, uploadPhotoBean)
            }
        }
        notifyDataSetChanged()
    }

    fun updateItemProgressByPath(localPath: String, percent: Double) {
        data.forEachIndexed { index, uploadPhotoBean ->
            if (localPath == uploadPhotoBean.localPath) {
                uploadPhotoBean.uploadPercent = percent
                notifyItemChanged(index, uploadPhotoBean)
            }
        }
        notifyDataSetChanged()
    }
}